﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Manger_ResourceType : System.Web.UI.Page
{
    string sMajorID;
    //根节点显示的名称
    string sMajorName;

    protected void Page_Load(object sender, EventArgs e)
    {

        HttpCookie ck = Request.Cookies["major"];
        if (ck != null)
        {
            sMajorID = ck.Values["majorid"];
            sMajorName = HttpUtility.UrlDecode(ck.Values["majorname"]);
            XmlDataSource1.DataFile = "~/xml/ResourceType_" + sMajorID + ".xml";

        }
        else
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        tbSelected.Text = TreeView1.SelectedNode.Text;

    }
    /// <summary>
    /// 更新节点名称
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btUpdate_Click(object sender, EventArgs e)
    {
        //存入数据库
        SqlDataSource1.UpdateCommand = "update [zyk_ResourceType] set [ResourceType_Name]=@resourcetype_name where [ResourceType_ID]=@resourcetype_id";
        SqlDataSource1.UpdateParameters.Add("resourcetype_id", TreeView1.SelectedNode.Value);
        SqlDataSource1.UpdateParameters.Add("resourcetype_name", tbSelected.Text);
        SqlDataSource1.Update();

        TreeView1.SelectedNode.Text = tbSelected.Text;
        SaveToXMLFile();

    }
    /// <summary>
    /// 增加新节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btNew_Click(object sender, EventArgs e)
    {
        //这里最后的专业需要修改

        SqlDataSource1.InsertCommand = "insert into[zyk_ResourceType]([ResourceType_Name],[ResourceType_Parent],[ResourceType_Major]) values(@resourcetype_name,@resourcetype_parent,@resourcetype_major)";
        SqlDataSource1.InsertParameters.Add("resourcetype_name", tbNewNode.Text);
        SqlDataSource1.InsertParameters.Add("resourcetype_parent", TreeView1.SelectedNode.Value);
        SqlDataSource1.InsertParameters.Add("resourcetype_major", sMajorID);
        SqlDataSource1.Insert();

        //查找出刚刚添加的那条记录号
        //这里不能用SCOPE_IDENTITY 和 @@IDENTITY 方式提取，这两种方案都需要在同一个会话中，而我们执行的是两个操作，不是同一个会话。
        SqlDataSource1.SelectCommand = "select max([ResourceType_ID]) from [zyk_ResourceType]";
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        string newid = dv[0][0].ToString();

        TreeNode td = new TreeNode();
        td.Text = tbNewNode.Text;
        td.Value = newid;
        TreeView1.SelectedNode.ChildNodes.Add(td);
        TreeView1.SelectedNode.ExpandAll();
        SaveToXMLFile();

        tbNewNode.Text = "";

    }
    /// <summary>
    /// 上移节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btMoveUp_Click(object sender, EventArgs e)
    {
        TreeNode MoveNode = TreeView1.SelectedNode;
        TreeNode ParentNode = MoveNode.Parent;
        int index = ParentNode.ChildNodes.IndexOf(MoveNode);
        //如果还不是该层次第一个节点，那就上移，如果是第一个了，那当然移不动了呀。
        if (index > 0)
        {
            ParentNode.ChildNodes.RemoveAt(index);
            ParentNode.ChildNodes.AddAt(index - 1, MoveNode);
            ParentNode.ChildNodes[index - 1].Select();
        }
        SaveToXMLFile();

    }
    /// <summary>
    /// 下移节点。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btMoveDown_Click(object sender, EventArgs e)
    {
        TreeNode MoveNode = TreeView1.SelectedNode;
        TreeNode ParentNode = MoveNode.Parent;
        int index = ParentNode.ChildNodes.IndexOf(MoveNode);
        //如果还不是该层次最后一个节点，那就下移，在尾巴上了当然没法移动了。
        if (index < ParentNode.ChildNodes.Count - 1)
        {
            ParentNode.ChildNodes.RemoveAt(index);
            ParentNode.ChildNodes.AddAt(index + 1, MoveNode);
            ParentNode.ChildNodes[index + 1].Select();
        }
        SaveToXMLFile();
    }
    /// <summary>
    /// 删除节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btDelete_Click(object sender, EventArgs e)
    {

        SqlDataSource1.DeleteCommand = "delete from [zyk_ResourceType] where [ResourceType_ID]=@resourcetype_id";
        SqlDataSource1.DeleteParameters.Add("resourcetype_id", TreeView1.SelectedNode.Value);
        SqlDataSource1.Delete();


        TreeNode DeleteNode = TreeView1.SelectedNode;
        TreeNode ParentNode = DeleteNode.Parent;
        int index = ParentNode.ChildNodes.IndexOf(DeleteNode);
        ParentNode.ChildNodes.RemoveAt(index);
        //被删除了父节点顶上
        ParentNode.Select();
        SaveToXMLFile();

    }
    /// <summary>
    /// 保存数据到XML文件
    /// </summary>
    protected void SaveToXMLFile()
    {
        //调用BuildXMLNode函数递归构造XML文档
        string XMLStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        XMLStr += "<siteMapNode typename=\"" + sMajorName + "\" typeid=\"0\">\n";
        int count = TreeView1.Nodes.Count;

        for (int i = 0; i < count; i++)
        {
            XMLStr += BuildXMLNode(TreeView1.Nodes[i]);
        }
        XMLStr += "</siteMapNode>";

        //将其保存起来
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(XMLStr);
        xd.Save(MapPath(XmlDataSource1.DataFile));

    }
    /// <summary>
    /// 递归调用构建节点
    /// </summary>
    /// <param name="tnCurrent"></param>
    /// <returns></returns>
    public string BuildXMLNode(TreeNode tnCurrent)
    {
        string XMLString = "";
        int count = tnCurrent.ChildNodes.Count;
        for (int i = 0; i < count; i++)
        {
            if (tnCurrent.ChildNodes[i].ChildNodes.Count > 0)
            {
                XMLString += "<siteMapNode typename=\"" + tnCurrent.ChildNodes[i].Text + "\" typeid=\"" + tnCurrent.ChildNodes[i].Value + "\">\n";
                XMLString += BuildXMLNode(tnCurrent.ChildNodes[i]);
                XMLString += "</siteMapNode>\n";
            }
            else
            {
                XMLString += "<siteMapNode typename=\"" + tnCurrent.ChildNodes[i].Text + "\" typeid=\"" + tnCurrent.ChildNodes[i].Value + "\" />\n";
            }

        }
        return XMLString;
    }

}
